package com.gsh.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gsh.config.JsonTest;
import com.gsh.pojo.Electricity;
import com.gsh.pojo.Voltage;
import com.gsh.service.IElectricityService;
import com.gsh.service.IVoltageService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@ResponseBody
public class VIController {

    private static final Logger LOGGER = LoggerFactory.getLogger(VIController.class);

    @Autowired
    IVoltageService voltageService;

    @Autowired
    IElectricityService electricityService;

    /**
     * 导入Excel表的接口
     */
    @PostMapping("/importExcel")
    public String singleFileUpload(@RequestParam("file") MultipartFile file,
                                   @RequestParam("type") String type) {
        //LOGGER.debug("开始上传Excel文件");
        JsonTest result = new JsonTest();
        try {
            if (file == null) {
                // 文件不能为空
                //LOGGER.info("file参数为空！");
                result.setCode(400);
                result.setMsg("文件为空，请重新上传！");
                return JSON.toJSONString(result);
            }

            String fileName = file.getOriginalFilename();
            if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
                // 文件格式不正确
                //LOGGER.info("Excel文件格式不正确！");
                result.setCode(400);
                result.setMsg("Excel文件格式不正确！");
                return JSON.toJSONString(result);
            }

            long size = file.getSize();
            if (StringUtils.isEmpty(fileName) || size == 0) {
                // 文件不能为空
                //LOGGER.info("Excel文件内容为空！");
                result.setCode(200);
                result.setMsg("Excel文件内容为空！");
                return JSON.toJSONString(result);
            }
//            if(type.equals("voltage")){//电压数据
//                result = voltageService.uploadExcel(fileName, file, result);
//            }else {//电流数据
//                result = electricityService.uploadExcel(fileName, file, result);
//            }
            result.setCode(200);
            result.setMsg("上传成功！");


        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(400);
            result.setMsg("上传出错，请重新上传!");
        }

        return JSON.toJSONString(result);
    }
    @PostMapping("/queryVI")
    public String queryV(@RequestParam(name = "measuringPoint") String measuringPoint,
                         @RequestParam(name = "beginDate") Date beginDate,
                         @RequestParam(name = "endDate") Date endDate,
                         HttpSession session,
                         HttpServletRequest request,
                         HttpServletResponse response){
        JsonTest result=new JsonTest();
        List<Voltage> voltages = voltageService.querybyMD(measuringPoint,1, beginDate, endDate);
        List<Electricity> electricities = electricityService.querybyMD(measuringPoint,1, beginDate, endDate);

        if(voltages.size()==electricities.size() && voltages.size()>0){
            // 保存cookie
            Cookie cookie1=new Cookie("measuringPoint",measuringPoint);
            Cookie cookie2=new Cookie("beginDate",beginDate.toString());
            Cookie cookie3=new Cookie("endDate",endDate.toString());



            result.setCode(200);
            result.setMsg("加载完毕！");
        }else {
            result.setCode(400);
            result.setMsg("该测量点及时间段下没有数据！");
        }
        return JSON.toJSONString(result);
    }

    /**
     * 查询电压数据
     */
    @PostMapping("/queryV")
    public String queryV(@RequestParam(name = "measuringPoint") String measuringPoint,
                          @RequestParam(name="errType") int errType,
                          @RequestParam(name = "beginDate") Date beginDate,
                          @RequestParam(name = "endDate") Date endDate){
        List<Voltage> voltages = voltageService.querybyMD(measuringPoint,errType, beginDate, endDate);
        return JSONObject.toJSONString(voltages);
    }

    @PostMapping("/queryI")
    public String queryI(@RequestParam(name = "measuringPoint") String measuringPoint,
                         @RequestParam(name="errType") int errType,
                          @RequestParam(name = "beginDate") Date beginDate,
                          @RequestParam(name = "endDate") Date endDate){
        List<Electricity> electricities = electricityService.querybyMD(measuringPoint,errType, beginDate, endDate);
        return JSONObject.toJSONString(electricities);
    }
}
